package com.enation.app.shop.front.api.jpush;

import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.Notification;
import com.enation.app.shop.front.api.jpush.entity.JPushContent;
import com.enation.app.shop.front.api.jpush.service.IJPushManager;
import com.enation.framework.database.IDaoSupport;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.List;
@Service("JPushService")
public class JPushManager implements IJPushManager {
    @Autowired
    private IDaoSupport daoSupport;


    private Logger logger = Logger.getLogger(getClass());
    @Override
    public int create(JPushContent jPushContent) {
        this.daoSupport.insert("rbt_jpush",jPushContent);
        return 1;
    }

    @Override
    public List<JPushContent> getByUserid(Integer userid) {
        String sql="select * from rbt_jpush where user_id=? order by time desc";
        return this.daoSupport.queryForList(sql,JPushContent.class,userid);
    }

    @Override
    public void JpushTest(Integer userid, String jpushregisid,Integer type) {

        JPushClient jPushClient=new JPushClient("0bef202b4ae3e9d48d890e45", "43baf9bf11dc2a751c98889b",null, cn.jiguang.common.ClientConfig.getInstance());
        PushPayload pushPayloads = buildPushObject_android_tag_alertWithTitle(userid,jpushregisid,type);
        //  List<PushPayload> payloads = buildPushObject_all_alias_alert_ios();
        try {
//            if (payloads != null) {
//                for (PushPayload payload : payloads) {
//
//                    PushResult result = jpushClient.sendPush(payload);
//                    logger.info("Got result - " + result);
//                }
//            }


                PushResult result = jPushClient.sendPush(pushPayloads);
                System.out.println(result);
                logger.info("Got result - " + result);




        } catch (APIConnectionException e) {
            // Connection error, should retry later
            logger.error("Connection error, should retry later", e);

        } catch (APIRequestException e) {
            // Should review the error, and fix the request
            logger.error("Should review the error, and fix the request", e);
            logger.info("HTTP Status: " + e.getStatus());
            logger.info("Error Code: " + e.getErrorCode());
            logger.info("Error Message: " + e.getErrorMessage());
        }
    }


    public  PushPayload buildPushObject_android_tag_alertWithTitle(Integer userid,String jpushregisid,Integer type) {
        if (type==1){
            String content="您的额度激活成功，可用额度为3000.";
            String title="额度激活";
            JPushContent jPushContent=new JPushContent();
            jPushContent.setContent(content);
            jPushContent.setTime(new Timestamp(System.currentTimeMillis()));
            jPushContent.setUser_id(userid);
            jPushContent.setType("激活");
            jPushContent.setTitle(title);
            create(jPushContent);
            PushPayload pushPayload=PushPayload.newBuilder()
                    .setPlatform(Platform.android())
                    .setAudience(Audience.registrationId(jpushregisid))
                    .setNotification(Notification.android(content, title, null))
                    .build();
            return pushPayload;
        }else if (type==2){
            String content="您的临时额度还有一天到期，请及时还款。";
            String title="及时还款";
            JPushContent jPushContent=new JPushContent();
            jPushContent.setContent(content);
            jPushContent.setTime(new Timestamp(System.currentTimeMillis()));
            jPushContent.setUser_id(userid);
            jPushContent.setType("还款");
            jPushContent.setTitle(title);
            create(jPushContent);
             PushPayload pushPayload=PushPayload.newBuilder()
                    .setPlatform(Platform.android())
                    .setAudience(Audience.registrationId(jpushregisid))
                    .setNotification(Notification.android(content, title, null))
                    .build();
            return pushPayload;
        }else {
            String content="您有新的退款到账通知,请在借还记录查看.";
            String title="商品退款";
            JPushContent jPushContent=new JPushContent();
            jPushContent.setContent(content);
            jPushContent.setTime(new Timestamp(System.currentTimeMillis()));
            jPushContent.setUser_id(userid);
            jPushContent.setType("退款");
            jPushContent.setTitle(title);
            create(jPushContent);
            PushPayload pushPayload=PushPayload.newBuilder()
                    .setPlatform(Platform.android())
                    .setAudience(Audience.registrationId(jpushregisid))
                    .setNotification(Notification.android(content, title, null))
                    .build();
            return pushPayload;
        }

    }

}
